package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.entity.User;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired(required = false)
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @param session
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        long userId = (long) session.getAttribute("user");
        shoppingCart.setUserId(userId);
       shoppingCart= shoppingCartService.add(shoppingCart);
        return R.success(shoppingCart);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session){
        long userId = (long) session.getAttribute("user");
       List<ShoppingCart> shoppingCartList= shoppingCartService.list(userId);

        return R.success(shoppingCartList);
    }
    @DeleteMapping("/clean")
    public R<String> clean (HttpSession session){
        long userid = (long) session.getAttribute("user");
        shoppingCartService.delete(userid);
        return R.success("清空完成");
    }

    /**
     * 删除购物车

     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart>  deleteByDishidOrSetmealid(@RequestBody ShoppingCart shoppingCart,HttpSession session){
//        Long userId = (Long) session.getAttribute("user");
        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);
        ShoppingCart shoppingCart1=  shoppingCartService.deleteByDishidOrSetmealid(shoppingCart);
        return R.success(shoppingCart1);

    }
}
